/**
 * Created by jt on 2017/5/6.
 */
import "./index.less";
import "../../../unit/common/js/bootstrap_js/jquery.min.js";
import "../../../unit/common/js/bootstrap_js/select2.min.js";
import "../../../unit/common/js/bootstrap_js/jquery.multi-select.js";
import "../../../unit/common/js/bootstrap_js/jquery.dataTables.js";
import "../../../unit/common/js/bootstrap_js/form-samples.js";
import "../../../unit/common/js/bootstrap_js/table-editable.js";
import "../../../unit/common/js/bootstrap_js/form-components.js";

jQuery(document).ready(function() {
    App.init();
    // FormComponents.init();
    var token = sessionStorage.getItem('token');
    if (token == null || token =='') {
        location.href = HOST_FRT+'/logins/login.html';
    }
    jQuery('.logOut').attr('href','../logins/login.html');
    var lnkId = getArgs().id;

    var lnkPic;
    var picFlag = true;
    jQuery.ajax({
        url: HOST_ADDR+'/cpmp/lnkInf/view/'+lnkId,
        type: 'get',
        // data: JSON.stringify(data),
        dataType: 'json',
        contentType:"application/json",
        headers: {
            'access_token': token,'login_type':'ums'
        },
        success: function(resp){
            if (!resp.success) {
                alert(resp.msgCod+' '+resp.msgInf);
                return;
            }
            console.log(resp);

            jQuery('.lnkNam').val(resp.data.lnkNam);
            jQuery('.lnkId').val(resp.data.lnkId);
            jQuery('.lnkUrl').val(resp.data.lnkUrl);
            jQuery('.lnkPic img').attr('src',HOST_FILE+resp.data.lnkPic);
            lnkPic = resp.data.lnkPic;

            //上传文件
            jQuery('.imgUpload').click(function () {
                // let file = $('.J_imgFile').get(0).files[0];
                picFlag = false;
                let formData = new FormData();
                // console.log(file);
                formData.append('file', $('.J_imgFile')[0].files[0]);
                console.log(formData);

                $.ajax({
                    url: HOST_ADDR+'/cpmp/file/upload/img',
                    type: 'post',
                    cache: false,
                    data: formData,
                    processData: false,
                    contentType: false,
                    success: function(resp){
                        if(resp.success){
                            // alert('上传文件成功');
                            console.log(resp);
                            lnkPic = resp.data;
                            jQuery('.lnkPic img').attr('src',HOST_FILE+lnkPic);
                        }else{
                            alert(resp.msgInf);
                        }
                        picFlag = true;
                    },
                    error:function(err){
                        console.log(err);
                    }

                })
            });
            
            //保存
            jQuery('.subKeep').click(function () {
                if (picFlag) {
                    let lnkNam = jQuery('.lnkNam').val();
                    let lnkId = jQuery('.lnkId').val();
                    let lnkUrl = jQuery('.lnkUrl').val();

                    if (lnkNam == '') {
                        alert('请填写合作商名称！');
                        return;
                    }
                    if (lnkUrl == '') {
                        alert('请选择链接地址！');
                        return;
                    }
                    if (lnkPic == '') {
                        alert('请添加链接图片！');
                        return;
                    }

                    let data = {
                        "pubFlg":'0',
                        "lnkNam":lnkNam,
                        "lnkId":lnkId,
                        "lnkUrl": lnkUrl,
                        "lnkPic": lnkPic
                    };

                    console.log(data);

                    jQuery.ajax({
                        url: HOST_ADDR+'/cpmp/lnkInf/edit',
                        type: 'post',
                        data: JSON.stringify(data),
                        dataType: 'json',
                        contentType:"application/json",
                        headers: {
                            'access_token': token,'login_type':'ums'
                        },
                        success: function(resp){
                            if (!resp.success) {
                                alert(resp.msgCod+' '+resp.msgInf);
                                return;
                            }
                            console.log(resp);
                            // //页面跳转到index
                            alert('保存成功');
                            location.href = '/portal_mng/partner_list.html';

                        },
                        error:function(err){
                            console.log(err);
                        }
                    });
                } else {
                    alert('图片上传中，请稍后提交！');
                }

            });

            //发布
            jQuery('.sharePro').click(function () {
                if (picFlag) {
                    let lnkNam = jQuery('.lnkNam').val();
                    let lnkId = jQuery('.lnkId').val();
                    let lnkUrl = jQuery('.lnkUrl').val();

                    if (lnkNam == '') {
                        alert('请填写合作商名称！');
                        return;
                    }
                    if (lnkUrl == '') {
                        alert('请选择链接地址！');
                        return;
                    }
                    if (lnkPic == '') {
                        alert('请添加链接图片！');
                        return;
                    }

                    let data = {
                        "pubFlg":'1',
                        "lnkNam":lnkNam,
                        "lnkId":lnkId,
                        "lnkUrl": lnkUrl,
                        "lnkPic": lnkPic
                    };

                    console.log(data);

                    jQuery.ajax({
                        url: HOST_ADDR+'/cpmp/lnkInf/edit',
                        type: 'post',
                        data: JSON.stringify(data),
                        dataType: 'json',
                        contentType:"application/json",
                        headers: {
                            'access_token': token,'login_type':'ums'
                        },
                        success: function(resp){
                            if (!resp.success) {
                                alert(resp.msgCod+' '+resp.msgInf);
                                return;
                            }
                            console.log(resp);
                            // //页面跳转到index
                            alert('发布成功');
                            location.href = '/portal_mng/partner_list.html';

                        },
                        error:function(err){
                            console.log(err);
                        }
                    });
                } else {
                    alert('图片上传中，请稍后提交！');
                }
            });
        },
        error:function(err){
            console.log(err);
        }
    });
    
});